<?php

namespace App\Http\Controllers;

use App\DataTables;
use App\Doctor;
use App\DoctorEdit;
use App\DoctorExpert;
use App\DoctorType;
use App\Http\Requests\DoctorExpertRequest;
use App\Http\Requests\DoctorTypeRequest;
use Illuminate\Http\Request;

/**
 * Class DoctorController
 * @package App\Http\Controllers
 */
class DoctorController extends Controller
{
    /**
     * DoctorController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 医生擅长领域
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function doctorExpert(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(DataTables::DoctorExpert());
        }
        return view('doctor.expert');
    }

    /**
     * 添加擅长领域
     * @param DoctorExpertRequest $request
     * @return array
     */
    public function doctorExpertPublish(DoctorExpertRequest $request)
    {
        return DoctorExpert::DoctorExpertPublish($request);
    }

    /**
     * 删除擅长领域
     * @param DoctorExpert $doctorExpert
     * @return array
     * @throws \Exception
     */
    public function doctorExpertDel(DoctorExpert $doctorExpert)
    {
        return $doctorExpert->delete() ? success('成功') : error('删除失败');
    }

    /**
     * 擅长领域显示状态修改
     * @param DoctorExpert $doctorExpert
     * @return array
     */
    public function doctorExpertModify(DoctorExpert $doctorExpert)
    {
        $doctorExpert->status = $doctorExpert->status === DoctorExpert::STATUS_OFF ? DoctorExpert::STATUS_ON : DoctorExpert::STATUS_OFF;
        return $doctorExpert->save() ? success('成功') : error('失败');
    }


    /**
     * 医生类型
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function doctorType(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(DataTables::DoctorType());
        }
        return view('doctor.type');
    }

    /**
     * 添加医生类型
     * @param DoctorTypeRequest $request
     * @return array
     */
    public function doctorTypePublish(DoctorTypeRequest $request)
    {
        return DoctorType::DoctorTypePublish($request);
    }

    /**
     * 删除医生类型
     * @param DoctorType $doctorType
     * @return array
     * @throws \Exception
     */
    public function doctorTypeDel(DoctorType $doctorType)
    {
        return $doctorType->delete() ? success('成功') : error('删除失败');
    }

    /**
     * 医生类型显示状态修改
     * @param DoctorType $doctorExpert
     * @return array
     */
    public function doctorTypeStatusModify(DoctorType $doctorType)
    {
        $doctorType->status = $doctorType->status === DoctorType::STATUS_OFF ? DoctorType::STATUS_ON : DoctorType::STATUS_OFF;
        return $doctorType->save() ? success('成功') : error('失败');
    }

    /**
     * 医生类型显示状态修改
     * @param DoctorType $doctorExpert
     * @return array
     */
    public function doctorTypeQualModify(DoctorType $doctorType)
    {
        $doctorType->needqualification = $doctorType->needqualification === DoctorType::QUAL_NO ? DoctorType::QUAL_YES : DoctorType::QUAL_NO;
        return $doctorType->save() ? success('成功') : error('失败');
    }

    /**
     * 医生管理
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function doctorList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(DataTables::DoctorList());
        }

        return view('doctor.doctorList');
    }

    /**
     * 删除医师
     * @param Doctor $doctor
     * @return array
     * @throws \Exception
     */
    public function doctorDel(Doctor $doctor)
    {
        $doctor->del = Doctor::DEL_ON;
        return $doctor->save() ? success('成功') : error('删除失败');
    }

    /**
     * 医师信息
     * @param Doctor $doctor
     * @return $this
     */
    public function doctorInfo(Doctor $doctor)
    {
        //如果有待审核的信息
        if ($doctor->status === Doctor::STATUS_NEW) {
            $info = DoctorEdit::select('person_name',
                'id_card',
                'sex',
                'age',
                'type',
                'qualification',
                'picconsult',
                'voiceconsult',
                'videoconsult',
                'expert',
                'introduction',
                'avatar',
                'card_up',
                'card_down',
                'qualificationpic',
                'otherpic',
                'createtime')
                ->find($doctor->editid);
        } else {
            $info = NULL;
        }

        return view('doctor.doctorInfo')->with(compact('doctor', 'info'));
    }


    /**
     * 医师审核通过或者新信息通过审核
     * @param Doctor $doctor
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOn(Doctor $doctor)
    {
        return response()->json(Doctor::StatusOn($doctor));
        $doctor->status = Doctor::STATUS_YES;
        if (!$doctor->save()) {
            return response()->json(error('失败'));
        }
        return response()->json(success('成功'));
    }


    /**
     * 医师不通过审核
     * @param Doctor $doctor
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOff(Doctor $doctor)
    {
        return response()->json(Doctor::StatusOFF($doctor));
        $doctor->status = Doctor::STATUS_REFUSE;
        if (!$doctor->save()) {
            return response()->json(error('失败'));
        }
        return response()->json(success('成功'));
    }


    /**
     * 更新信息
     * @param Doctor $doctor
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function newStatusOn(Doctor $doctor)
    {
        return response()->json(Doctor::NewInfoOn($doctor));
    }
}
